﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using KaraokeManagementBUS;
using KaraokeManagementDTO;

namespace KaraokeManagement
{
    public partial class frmPhieuTiepNhanPhong : DevComponents.DotNetBar.Office2007Form
    {

        private int MaPhieuTiepNhanPhong = -1;
        private int MaKhachHang = -1;

        public frmPhieuTiepNhanPhong()
        {
            InitializeComponent();
        }

        private void frmPhieuTiepNhanPhong_Load(object sender, EventArgs e)
        {
            dgvDanhSachHangHoa.AutoGenerateColumns = false;
            
            
            initGioiTinh();
            initLoaiKhachHang();
            initDanhSachHangHoa();
            initFormData();
            initHangHoa();


            if (ucQuanLyPhongHat.staticMaPhong != -1 && ucQuanLyPhongHat.staticTenPhong != "")
            {
                loadFormData();
            }

        }


        public void loadFormData()
        {
            txtPhong.Text = ucQuanLyPhongHat.staticTenPhong;

            if (ucQuanLyPhongHat.staticMaPhieuDatPhong != -1)
            {
                groupPanel1.Enabled = false;
                groupPanel3.Enabled = false;
                groupThongTinHangHoa.Enabled = true;
                dgvDanhSachHangHoa.Enabled = true;
                
                
                PhieuTiepNhanDatPhongDTO dto = new PhieuTiepNhanDatPhongDTO();
                dto = new PhieuTiepNhanDatPhongBUS().layThongTinPhieu(ucQuanLyPhongHat.staticMaPhieuDatPhong);

                MaPhieuTiepNhanPhong = dto.ID;

                //Load khách hàng
                loadKhachHang(dto.KhachHang_ID);

                //Load thông tin phòng
                txtGioVao.Text = dto.ThoiGianVao.Hour.ToString();
                txtPhutVao.Text = dto.ThoiGianVao.Minute.ToString();
                dateThoiGianVao.Value = dto.ThoiGianVao;

                //Load chi tiết phòng
                loadDanhSachCTPhieuDatPhong();
            }
        }

        public void loadKhachHang(int MaKhachHang)
        {
            KhachHangDTO dto = new KhachHangDTO();
            dto = new KhachHangBUS().layThongTinKhachHang(MaKhachHang);

            //Load dữ liệu form
            txtHoTen.Text = dto.HoTen;
            txtDiaChi.Text = dto.DiaChi;
            txtSoCMND.Text = dto.SoCMND;
            txtSoDienThoai.Text = dto.SoDienThoai;
            cbGioiTinh.SelectedText = dto.GioiTinh;
            cbLoaiKH.SelectedValue = dto.LoaiKhachHang_ID;            
        }

       

        public void initGioiTinh()
        {
            cbGioiTinh.Items.Add("Nam");
            cbGioiTinh.Items.Add("Nữ");
            cbGioiTinh.SelectedIndex = 0;
        }

        public void initLoaiKhachHang()
        {
            cbLoaiKH.DataSource = new LoaiKhachHangBUS().layDanhSachLoaiKhachHang();
            cbLoaiKH.DisplayMember = "TenLoaiKhachHang";
            cbLoaiKH.ValueMember = "ID";
        }

        public void initDanhSachHangHoa()
        {
            cbTenHangHoa.DataSource = new HangHoaBUS().layDanhSachHangHoa();
            cbTenHangHoa.DisplayMember = "TenHangHoa";
            cbTenHangHoa.ValueMember = "ID";
        }

        public void initFormData()
        {
            txtGioVao.Text = DateTime.Now.Hour.ToString();
            txtPhutVao.Text = DateTime.Now.Minute.ToString();
            dateThoiGianVao.Value = DateTime.Now;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            Boolean flag = true;

            if (MaKhachHang != -1)
            {
                KhachHangDTO dto = new KhachHangDTO();
                dto.HoTen = txtHoTen.Text;
                dto.GioiTinh = cbGioiTinh.SelectedItem.ToString();
                dto.SoDienThoai = txtSoDienThoai.Text;
                dto.DiaChi = txtDiaChi.Text;
                dto.SoCMND = txtSoCMND.Text;
                dto.LoaiKhachHang_ID = int.Parse(cbLoaiKH.SelectedValue.ToString());

                flag = true;

                if (this.MaPhieuTiepNhanPhong == -1)
                {
                    flag = new KhachHangBUS().themKhachHang(dto);
                }


                //Lấy mã khách hàng mới thêm vào
                MaKhachHang = new KhachHangBUS().layMaKhachHangMoiNhat();

            }

            PhieuTiepNhanDatPhongDTO phieuNhanDatPhongDTO = new PhieuTiepNhanDatPhongDTO();
            phieuNhanDatPhongDTO.KhachHang_ID = MaKhachHang;

            //Lấy h vào
            DateTime thoiGianVao = new DateTime(dateThoiGianVao.Value.Year,dateThoiGianVao.Value.Month,dateThoiGianVao.Value.Day,int.Parse(txtGioVao.Text),int.Parse(txtPhutVao.Text),0);
            phieuNhanDatPhongDTO.ThoiGianVao = thoiGianVao;
            
            //ID phòng
            phieuNhanDatPhongDTO.PhongHat_ID = ucQuanLyPhongHat.staticMaPhong;
            phieuNhanDatPhongDTO.TinhTrang = 1;


            if (this.MaPhieuTiepNhanPhong == -1)
            {
                flag = flag && (new PhieuTiepNhanDatPhongBUS().themPhieu(phieuNhanDatPhongDTO));
            }


            if (flag)
            {
                ucQuanLyPhongHat.staticMaPhong = -1;
                ucQuanLyPhongHat.staticTenPhong = "";

                groupThongTinHangHoa.Enabled = true;
                dgvDanhSachHangHoa.Enabled = true;
                
                //Cập nhật tình trạng phòng
                flag=flag&& (new PhongHatBUS().capNhatTinhTrangPhongHat(phieuNhanDatPhongDTO.PhongHat_ID, 1));
            }

        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThemVao_Click(object sender, EventArgs e)
        {
            ChiTietPhieuDatPhongDTO dto = new ChiTietPhieuDatPhongDTO();
            dto.HangHoa_ID = int.Parse(cbTenHangHoa.SelectedValue.ToString());
            dto.SoLuongDung = int.Parse(txtSoLuong.Text);
            dto.DonGiaBan = double.Parse(txtDonGia.Text);
            //dto.SoLuongKhach = int.Parse(txtSoLuongKhach.Text);
            dto.GiamGia = double.Parse(txtGiam.Text);
            dto.PhieuTiepNhanDatPhong_ID = new PhieuTiepNhanDatPhongBUS().layPhieuDatPhongMoiNhat();
            this.MaPhieuTiepNhanPhong = dto.PhieuTiepNhanDatPhong_ID;

            if (new ChiTietPhieuDatPhongBUS().themChiTiet(dto))
            {
                loadDanhSachCTPhieuDatPhong();
            }

        }

        public void loadDanhSachCTPhieuDatPhong()
        {
            if (this.MaPhieuTiepNhanPhong != -1)
            {
                dgvDanhSachHangHoa.DataSource = new ChiTietPhieuDatPhongBUS().layDanhSachChiTiet(this.MaPhieuTiepNhanPhong);
            }
        }


        public void initHangHoa()
        {
            dgvcbTenHangHoa.DataSource = new HangHoaBUS().layDanhSachHangHoa();
            dgvcbTenHangHoa.DisplayMember = "TenHangHoa";
            dgvcbTenHangHoa.ValueMember = "ID";
        }

        private void cbTenHangHoa_SelectedIndexChanged(object sender, EventArgs e)
        {

            try
            {
                if (cbTenHangHoa.SelectedValue != null)
                {
                    HangHoaDTO hh = new HangHoaDTO();
                    hh = (HangHoaDTO)cbTenHangHoa.SelectedValue;
                    txtDonGia.Text = hh.DonGia.ToString();
                }
            }
            catch(Exception ex)
            {
               
                if (cbTenHangHoa.SelectedValue != null)
                {                    
                    txtDonGia.Text = cbTenHangHoa.SelectedValue.ToString();
                }
            }
        }

        private void frmPhieuTiepNhanPhong_FormClosing(object sender, FormClosingEventArgs e)
        {
            ucQuanLyPhongHat.staticMaPhieuDatPhong = -1;
            ucQuanLyPhongHat.staticMaPhong = -1;
            ucQuanLyPhongHat.staticTenPhong = "";

        }

        private void btnThemKhachHangCu_Click(object sender, EventArgs e)
        {
            frmChonKhachHang frm = new frmChonKhachHang();
            frm.ShowDialog();

            if(frmChonKhachHang.staticMaKhachHang!=-1)
            {
                loadKhachHang(frmChonKhachHang.staticMaKhachHang);
                this.MaKhachHang = frmChonKhachHang.staticMaKhachHang;
            }
        }

        private void dgvDanhSachHangHoa_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvDanhSachHangHoa.SelectedRows.Count > 0)
            {
                int MaChiTiet=int.Parse(dgvDanhSachHangHoa.SelectedRows[0].Cells["ID"].Value.ToString());
                ChiTietPhieuDatPhongDTO dto = new ChiTietPhieuDatPhongDTO();
                dto = new ChiTietPhieuDatPhongBUS().layThongTinChiTiet(MaChiTiet);

                cbTenHangHoa.SelectedValue = dto.HangHoa_ID;
                txtDonGia.Text = dto.DonGiaBan.ToString();
                txtSoLuong.Text = dto.SoLuongDung.ToString();
                txtGiam.Text = dto.GiamGia.ToString();
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dgvDanhSachHangHoa.SelectedRows.Count > 0)
            { 
                int MaChiTiet=int.Parse(dgvDanhSachHangHoa.SelectedRows[0].Cells["ID"].Value.ToString());
                ChiTietPhieuDatPhongBUS bus = new ChiTietPhieuDatPhongBUS();

                bus.xoaChiTiet(MaChiTiet);

                loadDanhSachCTPhieuDatPhong();
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            if (dgvDanhSachHangHoa.SelectedRows.Count > 0)
            {

                ChiTietPhieuDatPhongDTO dto = new ChiTietPhieuDatPhongDTO();
                dto.HangHoa_ID = int.Parse(cbTenHangHoa.SelectedValue.ToString());
                dto.SoLuongDung = int.Parse(txtSoLuong.Text);
                dto.DonGiaBan = double.Parse(txtDonGia.Text);
                //dto.SoLuongKhach = int.Parse(txtSoLuongKhach.Text);
                dto.GiamGia = double.Parse(txtGiam.Text);
                dto.PhieuTiepNhanDatPhong_ID = this.MaPhieuTiepNhanPhong;
                dto.ID=int.Parse(dgvDanhSachHangHoa.SelectedRows[0].Cells["ID"].Value.ToString());

                ChiTietPhieuDatPhongBUS bus = new ChiTietPhieuDatPhongBUS();
                bus.capNhatChiTiet(dto);

                loadDanhSachCTPhieuDatPhong();
            }
        }


    }
}
